package com.maokeyang.messageboard.controller;

import com.maokeyang.messageboard.dto.MessageDto;
import com.maokeyang.messageboard.model.Message;
import com.maokeyang.messageboard.model.User;
import com.maokeyang.messageboard.service.MessageService;
import com.maokeyang.messageboard.util.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Controller
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 留言列表
     *
     * @param model
     * @param page
     * @return
     */
    @GetMapping({"/dashboard", "/dashboard.html"})
    public String dashboard(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> attributes = messageService.getMessageList(new PageRequest(page, 3));
        model.addAllAttributes(attributes);

        return "dashboard";
    }


    /**
     * 添加留言
     *
     * @param messageDto
     * @param request
     * @return
     */
    @PostMapping({"/message"})
    public String addMsg(@ModelAttribute("messageDto") MessageDto messageDto,
                         HttpServletRequest request,
                         RedirectAttributes attr) {
        Message message = new Message();
        message.setMessage(messageDto.getMessage());

        if (StringUtils.isEmpty(messageDto.getMessage())) {
            attr.addFlashAttribute("msg", "留言不能为空");
            return "redirect:/dashboard.html";
        }

        User user = (User) request.getSession().getAttribute("user");
        message.setUid(user.getId());

        try {
            messageService.addMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
            attr.addFlashAttribute("msg", "添加失败");
        }

        return "redirect:/dashboard.html";
    }

    /**
     * 编辑留言
     *
     * @param id
     * @param dto
     * @return
     */
    @PutMapping("/message/{id}")
    public String editMsg(@PathVariable("id") Integer id,  MessageDto dto,
                          RedirectAttributes attr) {
        Message message = new Message();
//        message.setUpdatedAt(new Date());
        message.setMessage(dto.getMessage());
        message.setId(id);
        Integer page = dto.getPage();

        if (StringUtils.isEmpty(dto.getMessage())) {
            attr.addFlashAttribute("msg", "留言不能为空");
            return "redirect:/dashboard.html";
        }

        try{
            messageService.updateMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
            attr.addFlashAttribute("msg", "编辑失败");
        }

        return "redirect:/dashboard?page=" + page;
    }

    /*
     * 删除留言
     */
    @DeleteMapping(value = "/message/{messageId}")
    public String deleteMessage(@PathVariable("messageId") Integer messageId) {

        messageService.delMessage(messageId);
        return "redirect:/dashboard.html";
    }
}
